<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script>
    // function runAsync(){
    //     var p = new Promise(function(resolve, reject){
    //         //做一些异步操作
    //         setTimeout(function(){
    //             console.log('执行完成');
    //             resolve('随便什么数据');
    //         }, 2000);
    //     });
    //     return p;
    // }
    // runAsync().then(function(data){
    //     console.log(data);
    //     //后面可以用传过来的数据做些其他操作
    //     //......
    // });


 /*   if (null) {
        console.log(1);
    }

    if (0) {
        console.log(2);
    }

    if (1) {
        console.log(3);
    }

    if (undefined) {
        console.log(4);
    }
*/

            function runAsync1() {
                return new Promise(function (resolve, reject) {
                    //做一些异步操作
                    setTimeout(function (str) {
                        console.log(str + '执行完成');
                        resolve()
                    }, 1000, '异步任务1');
                })
            }

            function runAsync2() {
                return new Promise(function (resolve, reject) {
                    //做一些异步操作
                    setTimeout(function (str) {
                        console.log(str + '执行完成');
                        //下面2句只会执行一句，谁先谁执行
                        reject('异步任务2')
                        resolve()
                    }, 1000, '异步任务2');
                })
            }

            function runAsync3() {
                return new Promise(function (resolve, reject) {
                    //做一些异步操作
                    setTimeout(function (str) {
                        console.log(str + '执行完成');
                    }, 1000, '异步任务3');
                })
            }

            runAsync1()
                .then(function (res) {
                    return runAsync2()
                },function (reason) {
                    console.log(reason,'捕捉出错1')
                })
                .then(function (res) {
                    return runAsync3()
                },function (reason) {
                    console.log(reason,'捕捉出错2')
                })
                .catch(function (reason) {
                    console.log(reason,'最后捕捉出错')
            })


</script>
</body>
</html>